#include <iostream>
#include <fstream>
#include "StringNode.h"
#include "FileNode.h"

FileNode* addToList(FileNode,FileNode*);
StringNode* createList(string);
int ifFound(StringNode *,string );
void printlist(StringNode*);

using namespace std;
int main()
{
	string file,loc,token;
	string dump[100];
	int dumpindex,counter;
	StringNode *Shead,*Send,*Stemp;
	FileNode *Fhead,*Fend,*Ftemp;
	Shead=Send=Stemp = NULL;
	Fhead=Fend=Ftemp = NULL;
	cout<<"\n Pls enter the name of file : ";
	cin>>file;
	cout<<"\n Pls enter the location of file : ";
	cin>>loc;
	Ftemp = new FileNode(file,loc);
	Fhead=addToList(*Ftemp,Fhead);
	
	
	
	
}
FileNode* addToList(FileNode fl,FileNode *header)
{
	FileNode *head,*end,*temp;
	head=header;
	temp=end=NULL;
	temp = new FileNode(fl.getFileName(),fl.getLocation());
	StringNode *s1;
	s1 = createList(fl.getFileName());
	temp->assignStringList(s1);

	if(head==NULL)
	{
		
		head=temp;
		end=head;
		
		
	}
	else
	{
		FileNode *dummy;
		dummy = header;
		while(dummy->downptr!=NULL)
		{
			dummy=dummy->downptr;
		}
		end = dummy;
		end->assignFileNode(temp);
		end=end->downptr;
	}
	return head;
	
}

StringNode * createList(string name)
{
	string token;
	ifstream infile(name.c_str(),ios::in);
	if(!infile)
	{
		cout<<"error whle opening the file\n";
	}
	else
	{
	//	string w;
		cout<<"file opened\n";
	//	while(!infile.eof())
	//		{
	//			infile>>w;
	//			cout<<"\n"<<w;	
	//		}
	}
	cout<<"flag";
	StringNode *head,*temp,*end;
	head=temp=end=NULL;
	while(!infile.eof())
	{
		infile>>token;
		if(!ifFound(head,token))
		{
			cout<<"\n new word\n";
			temp = new StringNode(token,0);
			if(head==NULL)
			{

				head=temp;
				end=head;
			
			}
			
			else
			{
				end->assignNextPtr(temp);
				end = end->rightptr;
				
			}
		}
		
		else
		{
			StringNode *dummy;
			dummy=head;
			while(dummy->rightptr!=NULL)
			{
				if(dummy->getWordName()==token)
				{
					dummy->incrementFrequency();
				}
			}
			
		}
	}


	printlist(head);	
	return head;	
}
int ifFound(StringNode *head,string keyword)
{

	while(head->rightptr!=NULL)
	{
		if(head->getWordName()==keyword)
		{
			return 1;
		}
		
	}
	return 0;
}
void printlist(StringNode *start)
{
	while(start->rightptr!=NULL)
	{
		cout<<"\n word :"<<start->getWordName()<<" freq :"<<start->getFrequency();
	}
}

